package com.resource.provider.serviceimpl;


import com.commons.utils.Result;
import com.commons.utils.ResultContent;
import com.commons.utils.ResultPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.resource.interfaces.interfacex.SurveyService;
import com.resource.interfaces.vo.Option;
import com.resource.interfaces.vo.Question;
import com.resource.interfaces.vo.Survey;
import com.resource.provider.dao.OptionMapper;
import com.resource.provider.dao.QuestionMapper;
import com.resource.provider.dao.SurveryMapper;
import com.resource.provider.exception.CoreException;
import com.resource.provider.exception.ParameterException;
import com.resource.provider.exception.PermissionException;
import com.shikonghualian.logicalhat.Param;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author try
 * @date 2020/06/29
 */
@Service
public class SurveyServiceImpl implements SurveyService {

    @Autowired
    private SurveryMapper surveryMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private OptionMapper optionMapper;

    @Override
    public Result getSurvey(String token, Integer surveyId) {

        try {
            Survey survey = surveryMapper.getSurvey(surveyId);

            List<Question> questions = questionMapper.getQuestionsBySurveyId(surveyId);

            for (Question question : questions) {

                List<Option> options = optionMapper.getOptionsByQuestionId(question.getQuestionId());
                question.setOptions(options);
            }
            survey.setQuestions(questions);
            return new ResultContent(0, "success", survey);
        } catch (
                PermissionException e) {
            e.printStackTrace();
            return new ResultContent(-1, "权限异常", e.getMessage());
        } catch (
                ParameterException e) {
            e.printStackTrace();
            return new ResultContent(-1, "参数异常", e.getMessage());
        } catch (
                CoreException e) {
            e.printStackTrace();
            return new ResultContent(-1, "无法找到资源", e.getMessage());
        } catch (
                DataAccessException e) {
            e.printStackTrace();
            return new ResultContent(-1, "sql语句错误", e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultContent(-1, "系统繁忙,请稍后再试", e.getMessage());
        }
    }


    @Override
    public ResultPage selectByInfo(@Param("token") String token, @Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize, @Param("surveyName") String surveyName, @Param("isRelease") Integer isRelease) {

        try {
            Map<String,Object> map = new HashMap<>();
            if(StringUtils.isNotBlank(surveyName)){
                map.put("surveyName","%"+surveyName+"%");
            }
            map.put("isRelease",isRelease);
            Page<Object> pageResult = PageHelper.startPage(pageIndex, pageSize, true);
            List<Survey> list = surveryMapper.selectByInfo(map);
            int pages, total;
            pages = pageResult.getPages();
            total = (int) pageResult.getTotal();
            return new ResultPage(0, "success", list, pageSize, pages, pageIndex, total);

        } catch (
                PermissionException e) {
            e.printStackTrace();
            return new ResultPage(-1, "权限异常", "", 0, 0, 0, 0);
        } catch (
                ParameterException e) {
            e.printStackTrace();
            return new ResultPage(-1, "参数异常", "", 0, 0, 0, 0);
        } catch (
                CoreException e) {
            e.printStackTrace();
            return new ResultPage(-1, "无法找到资源", "", 0, 0, 0, 0);
        } catch (
                DataAccessException e) {
            e.printStackTrace();
            return new ResultPage(-1, "sql语句错误", "", 0, 0, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultPage(-1, "系统繁忙,请稍后再试", "", 0, 0, 0, 0);
        }
    }


}
